package com.example.kotlintest.chat.listener;

import android.content.Context;
import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.kotlintest.chat.EmojiUtils;

/**
 * @author linzi
 * @date 2022-9-29
 */
public class ChatEditListener implements View.OnKeyListener{
    private static final String TAG = "ChatEditListener";
    private Context contextl;
    public ChatEditListener(Context context){
        this.contextl=context;
        Toast.makeText(contextl,"注册监听器",Toast.LENGTH_SHORT).show();
    }
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
//        Toast.makeText(contextl,"键盘事件==》"+keyCode+",焦点："+v.hasFocus(),Toast.LENGTH_SHORT).show();
        switch (keyCode){
            case KeyEvent.KEYCODE_DEL:
                if (event.getAction() == KeyEvent.ACTION_UP) {
                    break;
                }
                EditText view=(EditText)v;
                Editable edtxt=view.getText();
                Log.e(TAG, "onKey: length==>"+edtxt.length() );
                if(edtxt.length()==0){
                    break;
                }
                if(edtxt.length()==1){
                    edtxt.clear();
                    break;
                }
                int start=view.getSelectionStart();
                int end=view.getSelectionEnd();
                Log.e(TAG, "onKey1: "+start );
                Log.e(TAG, "onKey2: "+end );
                if(Math.abs(start-end)>0){
                    edtxt.delete(Math.min(start,end)+1,Math.max(start,end));
                    break;
                }
                edtxt.delete(start-1,start);
                Log.e(TAG, "onKey: "+edtxt );
                return true;
//                Log.e(TAG, "onKey3: "+edtxt.charAt(start - 1) );
//                char endPrefix = "]".charAt(0);
//                char startPrefix = "[".charAt(0);
//                int lastIndex = start-1;
//                if(endPrefix == edtxt.charAt(lastIndex)){
//                    Log.e(TAG, "onKeyDown: 上一个有找到]" );
//                    Toast.makeText(contextl,"匹配到表情",Toast.LENGTH_SHORT).show();
//                    int getStr= EmojiUtils.findIndexFromEnd(edtxt.toString(),lastIndex,startPrefix);
//                    Log.e(TAG, "onKey4: "+getStr );
//                    Log.e(TAG, "onKey5: "+lastIndex );
//                    String emojiStr=edtxt.toString().substring(getStr,lastIndex+1);
//                    Log.e(TAG, "onKey6: "+emojiStr );
//                    Toast.makeText(contextl,"匹配到表情:"+emojiStr,Toast.LENGTH_SHORT).show();
//                    if(EmojiUtils.getEmojiPosition(emojiStr)>-1||emojiStr.startsWith(EmojiUtils.filePrefixStart)){
//                        Toast.makeText(contextl,"执行删除",Toast.LENGTH_SHORT).show();
//                        edtxt.delete(getStr,lastIndex+1);
//                        Log.e(TAG, "onKey: 删除完成==>"+edtxt );
//                        return true;
//                    }
//                }
//                break;
        }
        return false;
    }
}
